package com.my.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.DefaultClassMapper;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.amqp.support.converter.SimpleMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Description TODO
 * @Author marshal
 * @Date 28/9/20 10:49 PM
 */
@Configuration
public class UserConfig {

    @Bean
    public Queue userInfo() {
        return new Queue("user.info");
    }

    @Bean
    public TopicExchange userTopicExchange() {
        return new TopicExchange("topic.user");
    }

    @Bean
    public Binding bindingTopicExchange() {
        return BindingBuilder.bind(userInfo()).to(userTopicExchange()).with("user.info");
    }

    /**
     * 修复无法解析实体类的问题
     * <p>
     * 参考: <a href="https://stackoverflow.com/questions/77499442/spring-boot-rabbitmq-attempt-to-deserialize-unauthorized-class-exception">pring Boot RabbitMQ attempt to deserialize unauthorized class exception</a>
     *
     * @return
     */
    @Bean
    public SimpleMessageConverter converter() {
        SimpleMessageConverter converter = new SimpleMessageConverter();
        converter.setAllowedListPatterns(List.of("com.my.bean.*", "java.util.*"));
        return converter;
    }
}
